<template>
  <Nav>
    {{ $t('dao.my') }}
  </Nav>
  <div class="page-dao-my">
    <div class="content">
      <template v-for="item in data">
        <div class="box animate__fadeIn animate__animated animate__faster">
          <div class="right animate__fadeIn animate__animated" :class="item.status">
            {{ $t('dao.status.' + item.status) }}
          </div>
          <div class="name wrap">{{ item.pledge.name }}</div>
          <div class="tongji">
            <div class="item">
              <div class="title">{{ $t('dao.poundage') }}</div>
              <div class="num self"><md-amount :value="item.poundage" :precision="0" transition></md-amount>%</div>
            </div>
            <div class="item">
              <div class="title">{{ $t('dao.weight') }}</div>
              <div class="num">{{ item.weight.name }}</div>
            </div>
            <div class="item">
              <div class="title">{{ $t('dao.exist_num') }} (HWD)</div>
              <div class="num"><md-amount :value="Number(item.count_num)" :precision="2" transition></md-amount></div>
            </div>
            <template v-if="item.status == 'ing'">
              <div class="item">
                <div class="title">{{ $t('dao.income_count') }} (HWD)</div>
                <div class="num"><md-amount :value="Number(item.income ?? 0)" :precision="2" transition></md-amount></div>
              </div>
              <div class="item" v-if="item.income_detail">
                <div class="title">{{ $t('dao.income') }} ({{ item.income_detail[0].date }})</div>
                <div class="num"><md-amount :value="Number(item.income_detail[0].value)" :precision="2" transition></md-amount></div>
              </div>
            </template>
            <div class="btn-group" v-if="item.ransom_status">
              <div class="btn" @click="onRansom(item)">{{ $t('dao.ransom') }}</div>
            </div>
          </div>
        </div>
      </template>
      <div class="page">
        <md-activity-indicator v-if="loading" type="carousel" color="#5d62a7" :size="15"></md-activity-indicator>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { toast, dialog } from '@/style'
import { t } from '@/lang'
import { log, ransom } from '@/api/dao'
import Nav from '@/components/nav.vue'

const router = useRouter()

const loading = ref(false)
const data = ref([])

// 加载数据
const getData = () => {
  loading.value = true
  log().then(res => {
    data.value = res.data
  }).finally(() => {
    loading.value = false
  })
}

// 赎回
const onRansom = data => {
  dialog.confirm({
    title: t('dao.ransom'),
    content: t('dao.ransom_message', { name: data.pledge.name }),
    confirmText: t('confirm'),
    onConfirm: async() => {
      toast(t('post'), 'ring', 0)
      ransom(data.id).then(res => {
        toast(t('ok'))
        getData()
      }).catch(res => {
        toast(res.message)
      })
    }
  })
}

getData()

</script>

<style lang="scss" scoped>
  .page-dao-my{
    min-height: 100%;
    background: url('@/assets/page.jpg') center center no-repeat;
    background-size: cover;
    background-attachment:fixed;

    .content{
      padding: 130px 30px 10px;
      overflow: hidden;
    }
    
    .page{
      padding: 20px;
      display: flex;
      align-items: center;
      flex-direction: column;
    }
    
    .tabs{
      background: rgba(255,255,255, .1);
      padding: 5px;
      border-radius: 100px;
      overflow: hidden;
      display: flex;
      margin-bottom: 30px;
      
      .item{
        flex-grow: 1;
        text-align: center;
        color: #5d62a7;
        line-height: 35px;
        border-radius: 100px;
        transition: all .4s ease;
        
        &.on{
          background: #5d62a7;
          color: #fff;
        }
      }
    }
    
    .create{
      background: linear-gradient(to right, #ffc21b, #ffa723);
      color: #fff;
      line-height: 40px;
      border-radius: 50px;
      text-align: center;
      font-size: 16px;
      width: 50%;
      margin: 0 auto 30px;
    }

    .box{
      padding: 15px 20px;
      margin-bottom: 20px;
      border-radius: 10px;
      background: linear-gradient(to left, #292d70, #1d0f5e);
      overflow: hidden;
      position: relative;
      transition: all .4s ease;
      
      .name{
        color: #fff;
        font-size: 16px;
        line-height: 25px;
        padding-right: 50px;
        overflow: hidden;
      }
      
      .right{
        position: absolute;
        top: 0;
        right: 0;
        background: rgba(0, 0, 0, 0.15);
        font-size: 12px;
        color: #5d62a7;
        padding: 4px 7px 4px 10px;
        border-radius: 0 0 0 10px;
        
        &.ing{
          color: #1cd4b6;
        }
        
        &.ransom{
          color: #ffc21b;
        }
      }
      
      .tongji{
        margin-top: 10px;
        
        .item{
          width: 100%;
          padding: 4px 0;
          display: flex;
          justify-content: space-between;
          
          .title{
            color: #5d62a7;
            font-size: 12px;
          }
          .num{
            color: #8991f9;
            font-size: 14px;
            font-weight: 400;
            
            &.self{
              color: #ffc21b;
            }
            
            &.on{
              color: #1cd4b6;
            }
          }
        }
      }
      
      .btn-group{
        display: flex;
        justify-content: center;
        margin-top: 15px;
      }
      
      .btn{
        background: linear-gradient(to right, #ffc21b, #ff9d23);
        color: #fff;
        width: 40%;
        line-height: 35px;
        font-size: 16px;
        border-radius: 35px;
        text-align: center;
        margin: 0 10px;
      }
    }
  }
</style>